package com.sskj.jc.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.sskj.jc.entity.DT_DaiJianJY;
import com.sskj.jc.entity.DT_Role;
import com.sskj.jc.entity.DaiJianListQueryParam;
import com.sskj.jc.entity.JianYanUpdateParam;
import com.sskj.jc.entity.ValidateRoleQueryParam;
import com.sskj.jc.repo.OrgRepositoryImpl;
import com.sskj.personnel.domain.entity.EmployeeEntity;
import com.sskj.personnel.repository.EmployeeRepository;
import com.sskj.utils.HttpHelp;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(value = "**/dingtalk/jc")
@Api(value = "检测钉钉服务", description = "检测钉钉服务")
public class JianCeServiceImpl {

	protected final Log logger = LogFactory.getLog(JianCeServiceImpl.class);

	/** 检测服务URL */
	@Value("${sskj.conf.tcmsServer}")
	private String tcmsServer;

	@Autowired
	EmployeeRepository employeeRepository;

	@Autowired
	OrgRepositoryImpl orgRepository;

	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/role", method = RequestMethod.GET)
	@ApiOperation(value = "校核权限", httpMethod = "GET")
	public List<DT_Role> getRole(ValidateRoleQueryParam params) {
		Map<String, String> roleMap = new HashMap<String, String>();
		roleMap.put("ws_xiaohe", "002201B005");
		roleMap.put("ws_shenpi", "002201B006");
		List<DT_Role> results = new ArrayList<DT_Role>();
		if (params != null) {
			logger.info("获取权限" + params.getDdid());
			for (String wsCode : roleMap.keySet()) {
				DT_Role role = new DT_Role();
				role.setRoleName(wsCode);
				String paramStr = "/" + params.getDdid() + "/" + roleMap.get(wsCode);
				try {
					String rs = HttpHelp.sendGet(tcmsServer + "/validateRole" + paramStr, null);
					JSONObject rsJsonStr = JSONObject.parseObject(rs);
					String code = rsJsonStr.getString("rmcode");
					if ("200".equals(code)) {
						String result = rsJsonStr.getString("msg");
						if ("true".equals(result)) {
							role.setTrueOrFalse(true);
						}
						role.setTrueOrFalse(true);
					}
				} catch (Exception e) {
					logger.info("获取权限失败。" + e.getMessage());
				}
				results.add(role);
			}
		} else {
			logger.info("缺少不要参数，无法获取权限。" + new ValidateRoleQueryParam().toString());
			for (String wsCode : roleMap.keySet()) {
				DT_Role role = new DT_Role();
				role.setRoleName(wsCode);
				results.add(role);
			}
		}
		logger.info("获取权限" + params.getDdid());
		logger.info("获取权限" + results);
		return results;
	}

	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/daijian", method = RequestMethod.GET)
	@ApiOperation(value = "检验列表查询", httpMethod = "GET")
	public List<DT_DaiJianJY> getDaiJianList(DaiJianListQueryParam params) {
		List<DT_DaiJianJY> results = new ArrayList<DT_DaiJianJY>();

		try {
			EmployeeEntity empEntity = employeeRepository.findByDD_Id(params.getDdid());

			if (empEntity != null) {

				String paramStr = "/" + params.getPageNum() + "/" + params.getRowCount() + "/"
						+ empEntity.getOrganization().getResteasy_org_id() + "/" + empEntity.getResteasy_emp_id() + "/"
						+ params.getWsCode();

				try {
					String rs = HttpHelp.sendGet(tcmsServer + "/getDaiJianList" + paramStr, null);
					results = JSONObject.parseArray(rs, DT_DaiJianJY.class);
					logger.info(results.size());
					logger.info(results);
				} catch (Exception e) {
					logger.info("获取待检列表失败。" + e.getMessage());
				}

			} else {
				logger.info("员工不存在。" + params.getDdid());
			}
		} catch (Exception e1) {
			logger.info("获取员工失败。" + e1.getMessage());
		}

		return results;
	}

	/**
	 * 校核.审批签字保存
	 */
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/updJianYan", method = RequestMethod.GET)
	@ApiOperation(value = "校核.审批签字保存", httpMethod = "GET")
	public boolean updateJianYan(JianYanUpdateParam params) {
		boolean result = false;

		EmployeeEntity empEntity = employeeRepository.findByDD_Id(params.getDdid());
		if (empEntity != null) {
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("wsCode", params.getWsCode());
			paramMap.put("empid", empEntity.getResteasy_emp_id());
			paramMap.put("tuiHui", params.getTuiHui());
			paramMap.put("jyids", params.getJyids());
			try {
				String rs = HttpHelp.sendPut(tcmsServer + "/updateJianYan", paramMap);
				JSONObject rsJsonStr = JSONObject.parseObject(rs);
				logger.debug(rs);
				String code = rsJsonStr.getString("rmcode");
				if ("200".equals(code)) {
					result = true;
				}
			} catch (Exception e) {
				logger.info("校核.审批签字保存失败。" + e.getMessage());
			}
		}

		return result;
	}

}
